package com.ai.module.business.service.impl;

import com.ai.module.chat.common.base.ThreadLocalContext;
import com.ai.module.business.domain.entity.BusinessRecordEntity;
import com.ai.module.business.domain.req.CommentBusinessReq;
import com.ai.module.chat.config.advice.exception.BusinessException;
import com.ai.module.business.mapper.BusinessRecordMapper;
import com.ai.module.business.mapper.BusinessTotalMapper;
import com.ai.module.business.service.LikeCollectRecordService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: MrHe
 * @create: 2024-01-25 02:04
 * @description:
 **/
@Service
public class LikeCollectRecordServiceImpl extends ServiceImpl<BusinessRecordMapper, BusinessRecordEntity> implements LikeCollectRecordService {

    @Autowired
    private BusinessTotalMapper businessTotalMapper;

    @Override
    @Deprecated
    public void collectLike(CommentBusinessReq commentBusinessReq) {
        String businessId = commentBusinessReq.getBusinessId();
        String businessType = commentBusinessReq.getBusinessType();
        BusinessRecordEntity commentBusinessEntity = this.baseMapper.selectOne(
                new LambdaQueryWrapper<BusinessRecordEntity>()
                        .eq(BusinessRecordEntity::getBusinessId, businessId)
                        .eq(BusinessRecordEntity::getUserId, ThreadLocalContext.getUserId())
                        .eq(BusinessRecordEntity::getBusinessType, businessType)
        );
        if (commentBusinessEntity !=null){
            removeById(commentBusinessEntity.getId());
            return;
        }
        Integer count =  businessTotalMapper.updateTotal(commentBusinessReq);
        if (count !=1){
            throw new BusinessException("数据异常,请待会儿再试");
        }
        BusinessRecordEntity businessRecord = new BusinessRecordEntity();
        BeanUtils.copyProperties(commentBusinessReq, businessRecord);
        this.save(businessRecord);
    }
}
